//
//  DateExtension.m
//  tjp_helper
//
//  Created by Daniel Braun on 30/01/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "DateExtension.h"


@implementation NSCalendarDate (DateExtension)

#define EVENING 19
#define MORNING 7

- (NSCalendarDate *) dateByNormalizingHours
{
	int dh=[self hourOfDay];
	dh=dh>=12 ? EVENING: MORNING;
	return [NSCalendarDate dateWithYear:[self yearOfCommonEra]
				      month:[self monthOfYear]
					day:[self dayOfMonth]
				       hour:dh minute:0 second:0 
				   timeZone:[self timeZone]];
}
- (NSCalendarDate *) dateBySettingMorningHour
{
	return [NSCalendarDate dateWithYear:[self yearOfCommonEra]
				      month:[self monthOfYear]
					day:[self dayOfMonth]
				       hour:MORNING minute:0 second:0 
				   timeZone:[self timeZone]];
}

- (NSCalendarDate *) dateBySettingEveningHour
{
	return [NSCalendarDate dateWithYear:[self yearOfCommonEra]
				      month:[self monthOfYear]
					day:[self dayOfMonth]
				       hour:EVENING minute:0 second:0 
				   timeZone:[self timeZone]];
}


- (NSCalendarDate *) dateByNormalizingWeekMonday
{
	NSCalendarDate *cd= [NSCalendarDate dateWithYear:[self yearOfCommonEra]
				      month:[self monthOfYear]
					day:[self dayOfMonth]
				       hour:MORNING minute:0 second:0 
				   timeZone:[self timeZone]];
	int wd=[cd dayOfWeek];
	int dof;
	if (wd<1) dof=1;	// sunday->next monday
	else if (wd==6) dof=2;	// saturday->next monday
	else dof=1-wd;
	return [cd dateByAddingYears:0
			      months:0 
				days:dof
			       hours:0
			     minutes:0
			     seconds:0];
	
}

- (NSCalendarDate *) dateByNormalizingWeekFriday
{
	NSCalendarDate *cd= [NSCalendarDate dateWithYear:[self yearOfCommonEra]
						   month:[self monthOfYear]
						     day:[self dayOfMonth]
						    hour:MORNING minute:0 second:0 
						timeZone:[self timeZone]];
	int wd=[cd dayOfWeek];
	int dof;
	if (wd<1) dof=1+5;	// sunday->next friday 
	else if (wd==6) dof=2+5;	// saturday->next friday
	else dof=5-wd;
	return [cd dateByAddingYears:0
			      months:0 
				days:dof
			       hours:0
			     minutes:0
			     seconds:0];
}


@end


